package everyday;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/6/18 22:38
 * 剑指 offer 57， 和为s的两个数字
 * https://leetcode.cn/problems/he-wei-sde-liang-ge-shu-zi-lcof/
 */
public class Solution57 {

    // 双指针
    public int[] twoSum(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int sum = nums[left] + nums[right];
            if (sum == target) {
                return new int[]{nums[left], nums[right]};
            } else if (sum > target) {
                --right;
            } else {
                ++left;
            }
        }
        return new int[0];
    }
}
